#ifndef TILTWIDGET_H
#define TILTWIDGET_H

#include <QWidget>
#include <QGraphicsView>
#include <QGraphicsPixmapItem>
#include <QMouseEvent>
#include <QPainter>
#include <QTimer>
#include <QtDebug>
#include "drunkscenemodel.h"
#include "mapdisplayitem.h"
#include "noise.h"

enum GameCondition{GAME_WAITING, GAME_PLAYING, GAME_WON, GAME_LOST};

const int FRAMES_BACKGROUND = 50;
const int FRAMES_WALKCYCLE = 20;
const int WALKCYCLE_FRAME_SPEED = 10;

const float VISIBLE_DISTANCE = 40.0;
const float LOD_SWITCHOVER = 24.0;

const float ROTATE_LEGS = 0.5;
const float ROTATE_TORSO = 0.4;
const float ROTATE_TORSO_ALT = 0.15;
const float ROTATE_HEAD = 0.3;
const float ROTATE_LEFT_ARM = 0.5;
const float ROTATE_LEFT_FOREARM = 0.5;
const float ROTATE_RIGHT_ARM = 0.5;
const float ROTATE_RIGHT_FOREARM = 0.5;

const float BOB_TORSO_AMPLITUDE = 4.0;
const float BOB_TORSO_WAVELENGTH = 2.0;
const float BOB_HEAD_AMPLITUDE = 2.0;
const float BOB_HEAD_WAVELENGTH = 4.3123;

// Animation target for falling to the left
const float TARGETL_LEGS = -80.0;
const float TARGETL_TORSO = -10.0;
const float TARGETL_LEFT_ARM = 80.0;
const float TARGETL_LEFT_FOREARM = 20.0;
const float TARGETL_HEAD = 20.0;
const float TARGETL_RIGHT_ARM = 75.0;
const float TARGETL_RIGHT_FOREARM = 10.0;

// Animation target for falling to the right
const float TARGETR_LEGS = 80.0;
const float TARGETR_TORSO = 10.0;
const float TARGETR_LEFT_ARM = 80.0;
const float TARGETR_LEFT_FOREARM = 30.0;
const float TARGETR_HEAD = -15.0;
const float TARGETR_RIGHT_ARM = 75.0;
const float TARGETR_RIGHT_FOREARM = 40.0;

// Animation target for victory
const float TARGETW_LEGS = 3.0;
const float TARGETW_TORSO = 2.0;
const float TARGETW_LEFT_ARM = 30.0;
const float TARGETW_LEFT_FOREARM = 40.0;
const float TARGETW_HEAD = -5.0;
const float TARGETW_RIGHT_ARM = -45.0;
const float TARGETW_RIGHT_FOREARM = -30.0;

const float LEGS_Y_POSITION = 250.0;

class TiltWidget : public QGraphicsView
{
    Q_OBJECT
public:
    explicit TiltWidget(DrunkSceneModel *m, QGraphicsScene *s,Noise *noise, QWidget *parent = 0);
    void setModel(DrunkSceneModel *m);
protected:
    void drawBackground(QPainter *painter, const QRectF &rect);
    void mouseMoveEvent(QMouseEvent *e);
    void mousePressEvent(QMouseEvent *e);
    void loadWalkcycleAnimation();
    void loadBackgroundAnimation();
    QPointF projectToScreenSpace(float posX, float posY);

    void loadObjectGraphics();
    void buildMap();
    void buildCharacter();
    void animateCharacter();

    DrunkSceneModel *model;
    QGraphicsScene *scene;
    // Segemnts of the character
    QGraphicsPixmapItem *legs;
    QGraphicsPixmapItem *torso;
    QGraphicsPixmapItem *leftArm;
    QGraphicsPixmapItem *leftForearm;
    QGraphicsPixmapItem *rightArm;
    QGraphicsPixmapItem *rightForearm;
    QGraphicsPixmapItem *head;

    MapDisplayItem *mapDisplay;

    QImage backgroundImages[FRAMES_BACKGROUND];
    QPixmap walkcycleImages[FRAMES_WALKCYCLE];
    QImage characterImage;

    QMap<QString, QImage> *objectsLoRes;
    QMap<QString, QImage> *objectsHiRes;

    float standUpProgress;
    int gameCondition;

    Noise *noise;

public slots:
    void updateScreen();
    void setDifficulty(int dif);
    void updateStandUpProgress(int time);
    void winGame(bool win, int time);
};

#endif // TILTWIDGET_H
